package com.gitee.ywj1352.gasc.temp;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TempDemo {
    //配置信息
    private static Configuration configuration = null;
    //模板文件的位置
    private static String tempPath = "C:\\Users\\yangwenjie.a\\Desktop\\test";

    static {
        if (configuration == null) {
            configuration = new Configuration(Configuration.VERSION_2_3_23);
            configuration.setDefaultEncoding("UTF-8");
            try {
                configuration.setDirectoryForTemplateLoading(new File(tempPath));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        List<JavaDoc> javaDoc = getJavaDoc();

        Template t = configuration.getTemplate("Docjava.ftl", "UTF-8");

        javaDoc.forEach(j->{
            String string = null;
            try {
                string = FreeMarkerTemplateUtils.processTemplateIntoString(t, j);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TemplateException e) {
                e.printStackTrace();
            }
            File file = new File("C:\\Users\\yangwenjie.a\\Desktop\\test\\ywj\\" + j.getJavaBeanName() +"Doc.java");
            try {
                FileUtils.writeStringToFile(file, string);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });


        System.out.println(javaDoc.size());
    }


    public static List<JavaDoc> getJavaDoc() throws IOException {
        InputStreamReader isr = new InputStreamReader(new FileInputStream("C:\\Users\\yangwenjie.a\\Desktop\\test\\BaseBookServerBook.jce"), "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        List<JavaDoc> javaDocs = new ArrayList<>();
        String line = null;
        int status = 0;
        JavaDoc javaDoc = null;
        List<JavaDoc.MethodDoc> methodDocs = null;
        while ((line = br.readLine()) != null) {
            if (StringUtils.isBlank(line)){
                continue;
            }
            if (line.indexOf("struct") != -1 && status == 0) {
                javaDoc = new JavaDoc();
                String pattern = "struct\\s+([a-z,A-Z]+)\\s+[{]";
                // 创建 Pattern 对象
                Pattern r = Pattern.compile(pattern);
                // 现在创建 matcher 对象
                Matcher m = r.matcher(line);
                if (m.find()) {
                    javaDoc.setJavaBeanName(m.group(1));
                }
                javaDocs.add(javaDoc);
                status = 1;
                continue;
            }
            if (status == 1) {
                String[] split = line.split(";\\s+[/][/]");
                JavaDoc.MethodDoc methodDoc = new JavaDoc.MethodDoc();
                methodDoc.setDescription(split[1].trim());
                String[] split1 = split[0].split("\\s+");
                String  methodName  = split1[split1.length - 1].trim().replaceAll("[=].*","");
                methodDoc.setMethod(captureName(methodName));
                String  rType  = split1[split1.length - 2].trim();
                rType =   rType.equals("string")?"String":rType;
                if (rType.indexOf("vector")!=-1){
                    rType = rType.replace("vector", "List");
                }
                methodDoc.setReturnType(rType);
                methodDocs = new ArrayList<>();
                methodDocs.add(methodDoc);
                javaDoc.setMethodList(methodDocs);
                status = 2;
                continue;
            }
            if (line.indexOf("}") == -1 && status == 2) {
                String[] split = line.split(";\\s+[/][/]");
                JavaDoc.MethodDoc methodDoc = new JavaDoc.MethodDoc();
                if (split.length >1){
                    methodDoc.setDescription(split[1].trim());
                }
                String[] split1 = split[0].split("\\s+");
                String  methodName  = split1[split1.length - 1].trim().replaceAll("[=].*","");
                String  rType  = split1[split1.length - 2].trim();
                rType =   rType.equals("string")?"String":rType;
                if (rType.indexOf("vector")!=-1){
                    rType = rType.replace("vector", "List");
                }
                methodDoc.setMethod(captureName(methodName));
                methodDoc.setReturnType(rType);
                methodDocs.add(methodDoc);
            }

            if (line.indexOf("}") != -1 && status == 2) {
                status = 0;
                continue;
            }
        }
        return javaDocs;
    }
    public static String captureName(String name) {
        char[] cs=name.toCharArray();
        if (!Character.isUpperCase(cs[0])){
            cs[0] = Character.toUpperCase(cs[0]);
        }
        String s = String.valueOf(cs);
        System.out.println(
                s
        );
        return s;
    }



}
